package com.qdu.mr.hbase.reduce;

import com.qdu.mr.CountMapper;
import com.qdu.mr.CountReducer;
import com.qdu.mr.hbase.CountHBaseMapper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class CountHBaseRunner {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 创建一个配置对象，存放Hadoop的job运行需要的所有配置信息
        // todo 配置对象需要提供HBase运行相关的参数
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","niit-master:2181");

        // 获取Job的实例
        Job job = Job.getInstance(conf);
        // 设置Job的jar的类
        job.setJarByClass(CountHBaseRunner.class);
        Scan scan = new Scan();
        scan.addColumn("f".getBytes(),"userid".getBytes());

        TableMapReduceUtil.initTableMapperJob(
                "t_log".getBytes(), // 要查询的表名
                scan,                    // 对表进行过滤的条件
                CountHBaseMapper.class,  // Mapper类
                Text.class,              // Mapper输出的Key的类型
                LongWritable.class,       // Mapper输出的Value的类型
                job,                     // 关联当前方法中的job对象
                true
        );

        TableMapReduceUtil.initTableReducerJob(
                "cls02wordcount",
                CountHBaseReducer.class,
                job
        );

        // 设置输入文件夹
        FileInputFormat.addInputPath(job,new Path("input"));

        // 启动job
        job.waitForCompletion(true);
    }
}
